﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace King.Engine.GameObjects.Hud.Menu
{
    internal class ListNode : IMenuNode
    {

        #region Fields

        private string _text;
        private string[] _items;
        private string _currentItem;
        private Action _itemSelected;
        private int _index;

        #endregion


        #region Constants

        private const string TextTemplate = "{0}    <-- {1} -->";

        #endregion


        #region Properties

        public MenuTree Parent { get; set; }

        public string Text
        {
            get
            {
                return String.Format(TextTemplate, _text, _currentItem);
            }
        }

        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        #endregion


        #region Constructor

        internal ListNode(string listName, Action<string> itemSelected, string[] items, int index)
        {
            _text = listName;
            _items = items;
            _index = index;
            _currentItem = _items[index];
            
            _itemSelected = (Action) (() => itemSelected(_currentItem));
        }

        #endregion


        #region Public Methods

        public void OnInput(MenuControl control)
        {
            switch (control)
            {
                case MenuControl.Left:
                    _index = MathUtil.TrueMod(_index - 1, _items.Length);
                    break;

                case MenuControl.Right:
                    _index = MathUtil.TrueMod(_index + 1, _items.Length);
                    break;

                default:
                    return;
            }

            _currentItem = _items[_index];

            _itemSelected();
        }

        public IEnumerator<IMenuNode> GetEnumerator()
        {
            throw new NotSupportedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}
